package com.cenwangkeji.resident.ui.activity;

import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.afollestad.materialdialogs.MaterialDialog;
import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.FamilyVerificationBean;
import com.cenwangkeji.resident.model.bean.ImgBean;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.cenwangkeji.resident.model.bean.User;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxTime;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
//添加家庭成员

public class AddFamilyActivity extends BaseActivity {

    @BindView(R.id.et_name)
    EditText mEtName;
    @BindView(R.id.et_id)
    EditText mEtId;
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_verification_code)
    EditText mEtVerificationCode;
    @BindView(R.id.btn_send)
    Button mBtnSend;
    private int mId;

    @Override
    public int setLayoutId() {
        return R.layout.activity_add_family;
    }


    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_send, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_send:
                final String name = mEtName.getText().toString();
                final String id = mEtId.getText().toString();
                final String phone = mEtPhone.getText().toString();
                if (TextUtils.isEmpty(name)){
                    MyToast.show("姓名不能空");
                }else  if (TextUtils.isEmpty(id)){
                    MyToast.show("身份证号不能为空");
                }else  if (TextUtils.isEmpty(phone)){
                    MyToast.show("手机号不能为空");
                }else {
                    send(phone,name,id);
                }

                break;
            case R.id.btn_add:
                add();
                break;
        }
    }

    private void send(final String phone, final String name , final String id){
        Observable.create(new ObservableOnSubscribe<SuccessBean>() {
            @Override
            public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                Gson gson = new Gson();
                map.put( Constant.token, User.getToken());
                map.put( "mark",3);
                map.put("xm",name);
                map.put("sfzh", id);
                map.put("brdh", phone);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.family_verification);
                MyLogger.i(json);
                FamilyVerificationBean verificationBean = gson.fromJson(json, FamilyVerificationBean.class);
                if (verificationBean.getCode() ==0) {
                    mId = verificationBean.getUserId();
                    map.clear();
                    map.put("mark", 3);
                    map.put("userId", User.getUserId());
                    map.put("token", User.getToken());
                    map.put("brdh", phone);
                    map.put("jtId", verificationBean.getUserId());
                    JSONObject jsonObjec = new JSONObject(map);
                    MyLogger.i(jsonObjec.toString());
                    String j = OkHttpUtils.post(jsonObjec.toString(), Constant.family_sms);
                    MyLogger.i(j);
                    emitter.onNext(gson.fromJson(j, SuccessBean.class));
                }else {
                    MyToast.show("没有找到该居民");
                }

            }
        }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
            @Override
            public void onNext(SuccessBean bean) {
                if (bean.getCode() == 0) {
                    mBtnSend.setClickable(false);
                    mBtnSend.setText("59s");
                    final RxTime time = new RxTime();
                    time.interval(1000, new RxTime.RxAction() {
                        @Override
                        public void action(long number) {
                            mBtnSend.setText(58-number+"s");
                            if (number==58){
                                mBtnSend.setClickable(true);
                                mBtnSend.setText("发送短信");
                                time.cancel();
                            }

                        }
                    });
                    MyToast.show("短信发送成功");

                } else {
                    MyToast.show(bean.getMsg());
                }
            }
        });
    }

    private void add(){
        final String name = mEtName.getText().toString();
        final String id = mEtId.getText().toString();
        final String phone = mEtPhone.getText().toString();
        final String code = mEtVerificationCode.getText().toString();
        if (TextUtils.isEmpty(name)){
            MyToast.show("姓名不能空");
        }else  if (TextUtils.isEmpty(id)){
            MyToast.show("身份证号不能为空");
        }else  if (TextUtils.isEmpty(phone)){
            MyToast.show("手机号不能为空");
        }else  if (TextUtils.isEmpty(code)){
            MyToast.show("短信验证码不能为空");
        }else {
            Observable.create(new ObservableOnSubscribe<SuccessBean>() {
                @Override
                public void subscribe(ObservableEmitter<SuccessBean> emitter) throws Exception {
                    Map<Object,Object> map = new HashMap<>();
                        map.put( Constant.token, User.getToken());
                        map.put( "mark",3);
                        map.put("userId",User.getUserId());
                        map.put("jtId",mId );
                        map.put("sms", code);
                        String j = OkHttpUtils.post((new JSONObject(map).toString()), Constant.add_family);
                        MyLogger.i(j);
                        Gson gson = new Gson();
                        SuccessBean successBean  = gson.fromJson(j, SuccessBean.class);
                        emitter.onNext(successBean);



                }
            }).compose(RxUtils.<SuccessBean>io_main()).subscribe(new ObserverOnNext<SuccessBean>() {
                @Override
                public void onNext(SuccessBean bean) {
                    if (bean.getCode()==0){
                        MyToast.show("添加成功");
                        finish();
                    }else {
                        MyToast.show(bean.getMsg());
                    }

                }
            });
        }
    }
}
